/* 
 * File:   TestFinder.cpp
 * Author: vicror
 * 
 * Created on December 3, 2011, 10:10 PM
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "TestFinder.h"
#include <fstream>
#include "../Vocabluary.h"

CPPUNIT_TEST_SUITE_REGISTRATION(TestFinderClass);

TestFinderClass::TestFinderClass() {
}

TestFinderClass::~TestFinderClass() {
}

void TestFinderClass::setUp() {
}

void TestFinderClass::tearDown() {
}

void TestFinderClass::testGeneral() {
    SearchEnginer::Finder se;
    SearchEnginer::Finder sa(se);


    CPPUNIT_ASSERT(se.append("a"));
    CPPUNIT_ASSERT(se.append("iii"));
    CPPUNIT_ASSERT(se.append("ii"));
    CPPUNIT_ASSERT(se.append("toutou"));
    CPPUNIT_ASSERT(se.append("teacher"));
    CPPUNIT_ASSERT(se.append("smaller"));
    CPPUNIT_ASSERT(se.append("nine"));
    CPPUNIT_ASSERT(se.append("to"));
    CPPUNIT_ASSERT(se.append("один"));
    CPPUNIT_ASSERT(se.append("два"));

    CPPUNIT_ASSERT(!sa.append("a"));
    CPPUNIT_ASSERT(!sa.append("iii"));
    CPPUNIT_ASSERT(!sa.append("ii"));
    CPPUNIT_ASSERT(!sa.append("toutou"));
    CPPUNIT_ASSERT(!sa.append("teacher"));
    CPPUNIT_ASSERT(!sa.append("smaller"));
    CPPUNIT_ASSERT(!sa.append("to"));
    CPPUNIT_ASSERT(!sa.append("один"));
    CPPUNIT_ASSERT(!sa.append("два"));


    SearchEnginer::ListString list;
    list = se.hasWord("a");
    CPPUNIT_ASSERT(list.size() == 1);
    CPPUNIT_ASSERT(list[0] == "a");
    list = sa.hasWord("ii");
    CPPUNIT_ASSERT(list.size() == 1);
    CPPUNIT_ASSERT(list[0] == "ii");
    list = sa.hasWord("iii");
    CPPUNIT_ASSERT(list.size() == 1);
    CPPUNIT_ASSERT(list[0] == "iii");
    list = sa.hasWord("tou");
    CPPUNIT_ASSERT(list.size() == 0);
    list = sa.hasWord("toutou");
    CPPUNIT_ASSERT(list.size() == 1);
    CPPUNIT_ASSERT(list[0] == "toutou");
    list = sa.hasWord("один");
    CPPUNIT_ASSERT(list.size() == 1);
    CPPUNIT_ASSERT(list[0] == "один");

    SearchEnginer::Finder sb;
    SearchEnginer::Finder sc(sb);


    CPPUNIT_ASSERT(sb.append("lock"));
    CPPUNIT_ASSERT(sb.append("поиск"));
    CPPUNIT_ASSERT(sb.append("замена"));
    CPPUNIT_ASSERT(sb.append("ключ"));
    CPPUNIT_ASSERT(sb.append("вставка"));
    CPPUNIT_ASSERT(sb.append("лямбдалямбда"));
    CPPUNIT_ASSERT(sb.append("лялька"));
    CPPUNIT_ASSERT(sb.append("лола"));
    CPPUNIT_ASSERT(sb.append("имярек"));
    CPPUNIT_ASSERT(sb.append("деньги"));
    list = sc.hasWord("лола");
    CPPUNIT_ASSERT(list.size() == 1);
    CPPUNIT_ASSERT(list[0] == "лола");
    list = sb.hasWord("ii");
    CPPUNIT_ASSERT(list.size() == 0);

    SearchEnginer::Finder sg;
    sg.append(se);
    list = sg.hasWord("a");
    CPPUNIT_ASSERT(list.size() == 1);
    CPPUNIT_ASSERT(list[0] == "a");
    list = sg.hasWord("ii");
    CPPUNIT_ASSERT(list.size() == 1);
    CPPUNIT_ASSERT(list[0] == "ii");
    list = sg.hasWord("iii");
    CPPUNIT_ASSERT(list.size() == 1);
    CPPUNIT_ASSERT(list[0] == "iii");
    list = sg.hasWord("tou");
    CPPUNIT_ASSERT(list.size() == 0);
    list = sg.hasWord("toutou");
    CPPUNIT_ASSERT(list.size() == 1);
    CPPUNIT_ASSERT(list[0] == "toutou");
    list = sg.hasWord("один");
    CPPUNIT_ASSERT(list.size() == 1);
    CPPUNIT_ASSERT(list[0] == "один");

    sa.unLink();
    CPPUNIT_ASSERT(sa.append("a"));
    CPPUNIT_ASSERT(sa.append("iii"));
    CPPUNIT_ASSERT(sa.append("ii"));
    CPPUNIT_ASSERT(sa.append("toutou"));
    CPPUNIT_ASSERT(sa.append("teacher"));
    CPPUNIT_ASSERT(sa.append("smaller"));
    CPPUNIT_ASSERT(sa.append("to"));
    CPPUNIT_ASSERT(sa.append("один"));
    CPPUNIT_ASSERT(sa.append("два"));
    sa.unLink();
    sa.link(se);
    CPPUNIT_ASSERT(!sa.append("a"));
    CPPUNIT_ASSERT(!sa.append("iii"));
    CPPUNIT_ASSERT(!sa.append("ii"));
    CPPUNIT_ASSERT(!sa.append("toutou"));
    CPPUNIT_ASSERT(!sa.append("teacher"));
    CPPUNIT_ASSERT(!sa.append("smaller"));
    CPPUNIT_ASSERT(!sa.append("to"));
    CPPUNIT_ASSERT(!sa.append("один"));
    CPPUNIT_ASSERT(!sa.append("два"));

}

void TestFinderClass::testIO() {
       SearchEnginer::Finder sg;
    CPPUNIT_ASSERT(sg.append("a"));
    CPPUNIT_ASSERT(sg.append("iii"));
    CPPUNIT_ASSERT(sg.append("ii"));
    CPPUNIT_ASSERT(sg.append("toutou"));
    CPPUNIT_ASSERT(sg.append("teacher"));
    CPPUNIT_ASSERT(sg.append("smaller"));
    CPPUNIT_ASSERT(sg.append("nine"));
    CPPUNIT_ASSERT(sg.append("to"));
    CPPUNIT_ASSERT(sg.append("один"));
    CPPUNIT_ASSERT(sg.append("два"));
    CPPUNIT_ASSERT(sg.append("lock"));
    CPPUNIT_ASSERT(sg.append("поиск"));
    CPPUNIT_ASSERT(sg.append("замена"));
    CPPUNIT_ASSERT(sg.append("ключ"));
    CPPUNIT_ASSERT(sg.append("вставка"));
    CPPUNIT_ASSERT(sg.append("лямбдалямбда"));
    CPPUNIT_ASSERT(sg.append("лялька"));
    CPPUNIT_ASSERT(sg.append("лола"));
    CPPUNIT_ASSERT(sg.append("имярек"));
    CPPUNIT_ASSERT(sg.append("деньги"));


    std::fstream os;
    os.open("basic1.dic",  std::fstream::out | std::fstream::trunc );
    os << sg;
    os.close();

    sg.unLink();
    SearchEnginer::Finder sc(sg);

    os.open("basic1.dic", std::fstream::in);
    os >> sc;
    os.close();
    CPPUNIT_ASSERT(!sg.append("a"));
    CPPUNIT_ASSERT(!sg.append("iii"));
    CPPUNIT_ASSERT(!sg.append("ii"));
    CPPUNIT_ASSERT(!sg.append("toutou"));
    CPPUNIT_ASSERT(!sg.append("teacher"));
    CPPUNIT_ASSERT(!sg.append("smaller"));
    CPPUNIT_ASSERT(!sg.append("nine"));
    CPPUNIT_ASSERT(!sg.append("to"));
    CPPUNIT_ASSERT(!sg.append("один"));
    CPPUNIT_ASSERT(!sg.append("два"));
    CPPUNIT_ASSERT(!sg.append("lock"));
    CPPUNIT_ASSERT(!sg.append("поиск"));
    CPPUNIT_ASSERT(!sg.append("замена"));
    CPPUNIT_ASSERT(!sg.append("ключ"));
    CPPUNIT_ASSERT(!sg.append("вставка"));
    CPPUNIT_ASSERT(!sg.append("лялька"));
    CPPUNIT_ASSERT(!sg.append("лола"));
    CPPUNIT_ASSERT(!sg.append("имярек"));
    CPPUNIT_ASSERT(!sg.append("деньги"));
}
